//
//  SetGestureController.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/6/13.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit
import LocalAuthentication

class SetGestureController: UIViewController {
    
    // 已经绘制了几次手势
    var times = 0
    var currentPwd = [Int]()
    
    var message: String! {
        didSet {
            messageLabel.text = message
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        layoutUI()
    }
    
    fileprivate func prepareUI() {
        message = "请设置手势密码"
        self.view.backgroundColor = UIColor.la_app
        self.view.addSubview(appIcon)
        self.view.addSubview(messageLabel)
        self.view.addSubview(lockView)
        self.view.addSubview(resetBtn)
        
    }
    
    fileprivate func layoutUI() {
        
        appIcon.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
            make.width.equalTo(appIcon.snp.height)
            make.width.equalTo(100)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(appIcon.snp.bottom).offset(20)
        }
        
        lockView.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom).offset(30)
            make.left.right.equalToSuperview()
            make.height.equalTo(lockView.snp.width)
        }
        
        resetBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-5)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
    }
    
    // MARK: 忘记手势密码，则重新登录
    func forgetCode() {
        self.dismiss(animated: true, completion: nil)
    }
    
    func resetGesture() {
        times = 0
        currentPwd = [Int]()
        message = "请设置手势密码"
    }
    
    lazy var appIcon: UIImageView = {
        let object = UIImageView()
        object.backgroundColor = UIColor.la_app
        object.image = UIImage(named: "app_icon_white.png")
        object.contentMode = .scaleToFill
        return object
    }()
    
    // messageLabel
    lazy var messageLabel: UILabel = {
        let object = UILabel()
        object.textColor = .white
        object.font = UIFont.la_Font(ofSize: 12)
        object.textAlignment = .center
        object.text = ""
        return object
    }()
    
    // 重新绘制按钮
    fileprivate lazy var resetBtn: UIButton = {
        let object = UIButton()
        object.setTitle("重新绘制", for: .normal)
        object.titleLabel?.font = UIFont.la_Font(ofSize: 13)
        object.titleLabel?.textAlignment = .left
        object.setTitleColor(UIColor.white, for: .normal)
        object.addTarget(self, action: #selector(resetGesture), for: .touchUpInside)
        return object
    }()
    
    fileprivate lazy var lockView: LockView = { [unowned self] in
        let object =  LockView()
        object.backgroundColor = UIColor.la_app
        object.delegate = self
        return object
        }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension SetGestureController: LockViewDelegate {
    
    func didFinished(_ isSuccess: Bool, title: String, message: String, password: [Int]) {
        lockView.reset()
        if isSuccess {
            // 绘制成功
            times += 1
            if times == 1 {
                // 第一次 绘制
                self.message = "请再次绘制手势密码"
                currentPwd = password
            } else {
                if currentPwd == password {
                    self.message = "手势密码设置成功"
                    Defaults[.setGestureLock] = true
                    Defaults[.gesturePWD] = password
                    let confirm = confirmDialog(title: "温馨提示", message: "是否设置指纹解锁", sure: { 
                        Defaults[.setFingerUnlock] = true
                        self.dismiss(animated: true, completion: nil)
                    }, cancle: {
                        self.dismiss(animated: true, completion: nil)
                    })
                    self.present(confirm, animated: true, completion: nil)
                } else {
                    self.message = "两次手势绘制不一致，请重新绘制"
                }
            }
        } else {
            messageLabel.text = message
        }
    }
}
